package com.aphrodite.ui.widgets;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

import com.aphrodite.ui.bg.Background;
import com.aphrodite.ui.graphics.Gradient;
import com.aphrodite.ui.graphics.GraphicEffects;
import com.aphrodite.util.BitmapFont;

/**
 * Contains Widget' theme information
 * 
 * @author kentelt
 * 
 */
public final class Theme {

	static int baseCmpColor = 0;
	private static Theme defTheme;

	private static final Font genericFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);

	public static Theme getDefault() {
		if (defTheme == null)
			defTheme = new Theme();
		return defTheme;
	}

	/**
	 * Color for base components in a widget
	 * <p>
	 * Color of the option circle OptionButtonGroup, Border color for a Button,
	 * Tick Color for checkbox,
	 * </p>
	 * 
	 * @param color
	 */
	public static void setBaseCmpColor(int color) {
		baseCmpColor = color;
	}

	private Background bg;
	int bgColor = 0xC6C3C6;
	BitmapFont bmFont;
	int borderColor = 0xADAAAD;
	boolean canDrawBorder = true;
	int fgColor = 0x000000;
	int highLightColor = 0x7B0000;

	int highLightFGColor = 0xffffff;
	boolean isTransparent;
	int titleColor;
	Font titleFont;
	private Gradient titleGradient;

	Font widgetFont;

	/**
	 * Create a Theme with default values
	 */
	public Theme() {
		this(genericFont, genericFont, null);
	}

	/**
	 * 
	 * @param bmFont
	 *            Bitmap font to be used by the widget
	 * @param titleFont
	 *            Font to be used by the widget title
	 */
	public Theme(BitmapFont bmFont, Font titleFont) {
		this(null, titleFont, bmFont);
	}

	/**
	 * Create a Theme with Font types specified
	 * 
	 * @param widgetFont
	 *            Font to be used by the widget
	 * @param titleFont
	 *            Font to be used by the widget title
	 */
	public Theme(Font widgetFont, Font titleFont) {
		this(widgetFont, titleFont, null);
	}

	private Theme(Font widgetFont, Font titleFont, BitmapFont bmFont) {
		this.widgetFont = widgetFont;
		this.titleFont = titleFont;
		this.bmFont = bmFont;
	}

	public boolean canDrawBorder() {
		return canDrawBorder;
	}

	/**
	 * @return widget background
	 */
	public final Background getBackground() {
		return bg;
	}

	/**
	 * Background color if the background for the widget is NULL see
	 * {@link Background}
	 */
	public final int getBGColor() {
		return bgColor;
	}

	public final int getBorderColor() {
		return borderColor;
	}

	/**
	 * 
	 * @return foreground color of the widget
	 */
	public final int getFGColor() {
		return fgColor;
	}

	/**
	 * Returns the current font used by the widget;
	 * 
	 * @return current font used by the widget
	 */
	public final Font getTitleFont() {
		return titleFont;
	}

	public final Font getWidgetFont() {
		return widgetFont;
	}

	public final boolean isTransparent() {
		return this.isTransparent;
	}

	final void renderBG(Graphics g, int width, int height) {
		/* Double level checking to draw BG */
		if (!isTransparent) {
			if (bg == null) {
				g.setColor(bgColor);
				g.fillRect(0, 0, width, height);
			} else {
				bg.setBounds(0, 0, width, height);
				bg.render(g);
			}
		}
	}

	final void renderBorder(Graphics g, int width, int height, int titleHeight) {
		if (!isTransparent && canDrawBorder) {
			g.setColor(borderColor);
			g.drawRect(0, 0, width - 1, height + titleHeight - 1);
		}
	}

	final void renderTitle(Graphics g, String title, int titleWidth, int titleHeight) {
		if (titleGradient != null) {
			GraphicEffects.drawGradient(g, titleGradient, 0, 0, titleWidth, titleHeight);
		} else {
			g.setColor(bgColor);
			g.fillRect(0, 0, titleWidth, titleHeight);
		}
		g.setColor(titleColor);
		g.setFont(titleFont == null ? widgetFont : titleFont);
		g.drawString(title, 2, 2, 0);
	}

	public final void setBackground(Background bg) {
		this.bg = bg;
	}

	/**
	 * Set the background color of this theme
	 * 
	 * @param bgColor
	 *            in hex
	 */
	public final void setBGColor(int bgColor) {
		this.bg = null;
		this.isTransparent = false;
		this.bgColor = bgColor;
	}

	/**
	 * Set the border color of this widget
	 * <p>
	 * If you need no border, set the border color to the widget background
	 * color.
	 * </p>
	 * 
	 * @param borderColor
	 *            in hex
	 */
	public final void setBorderColor(int borderColor) {
		this.borderColor = borderColor;
	}

	/**
	 * compute the height of the title using the current title font.
	 */

	public void setDrawBorder(boolean drawBorder) {
		this.canDrawBorder = drawBorder;
	}

	/**
	 * Set the foreground color of this widget
	 * 
	 * @param fgColor
	 *            in hex
	 */
	public final void setFGColor(int fgColor) {
		this.fgColor = fgColor;
	}

	/**
	 * Used for widgets that use highlighting. eg: {@link List}
	 * 
	 * 
	 * @param color
	 */
	public void setHighLightColor(int color) {
		this.highLightColor = color;
	}

	/**
	 * Set the FG color of an item when highlighted
	 * 
	 * @param color
	 */
	public void setHighLightFGColor(int color) {
		this.highLightFGColor = color;
	}

	/**
	 * Set the color of the title text
	 * 
	 * @param color
	 * @see Widget#setTitle(String)
	 * 
	 */
	public final void setTitleColor(int color) {
		this.titleColor = color;
	}

	/**
	 * Set the Gradient for the title
	 * 
	 * @see Widget#setTitle(String)
	 * @param gradient
	 */
	public final void setTitleGradient(Gradient gradient) {
		this.titleGradient = gradient;
	}

	public void setTransparent(boolean isTransparent) {
		this.isTransparent = isTransparent;
	}
}
